<?php

namespace app\admin\model;

use think\Db;
use app\admin\model\Common;
use think\Session;
use think\Validate;

class  Traindata extends Common
{  

  // 获取培训详情
  public function detail($id)
  {
    $data = Db::table('oa_admin_traindata')->where('id', $id)->find();
    return $data;
  }
//----------------辅导员-----------------

  //辅导员申请培训
  public function ApplyTraindata($param)
  {         
    $idnumber=$param['username'];
    $train_id=Db::table('oa_admin_train')
            ->where('idnumber',$idnumber)
            ->field('id')
            ->find();
    $res=Db::table('oa_admin_train')
        ->where('id',$train_id['id'])
        ->where('status',2)
        ->where('status1',2)
        ->find(); 
  if($res){
      $info['train_id']=$train_id['id'];
      $info['traindata_id']=$param['traindata_id'];
      $result=Db::table('oa_admin_checkno')
               ->where($info)
               ->select();
      if($result)
          {
           $this->error = '请勿重复申请同一期培训！！！';
           return false;
          }else{
                 $info['status']=1;
                 $info['status1']=1;
               Db::table('oa_admin_checkno')
                  ->insert($info);
              return true;
           }
         }else{
           $this->error = '未完成个人审核，没有申请资格';
           return false;
         }
    }
    //辅导员查看自己的培训信息
    public function ListtrainTraindata($param)
    {
      $train_id=Db::table('oa_admin_train')
               ->where('idnumber',$param['idnumber'])
               ->field('id')
               ->find();
     $data=Db::table('oa_admin_checkno')
              ->where('train_id',$train_id['id'])
              /* ->where('status',2)
              ->where('status1',2) */
              ->field('train_id,traindata_id,status,status1')
              ->paginate(config('pagesize'), false, ['page' => $page]);
     $temp1 = $data->toArray();
     for($i=0;$i<count($data);$i++){
       $temp[$i]=$temp1['data'][$i]['traindata_id'];
     }
     $traindata=Db::table('oa_admin_traindata')
               ->where('id',"in",$temp)
               ->select();
     for($i=0;$i<count($data);$i++)
     {
       for($j=0;$j<count($traindata);$j++)
       {
         if($temp1['data'][$i]['traindata_id']==$traindata[$j]['id'])
         {
           $temp1['data'][$i]['issue']=$traindata[$j]['issue'];
           $temp1['data'][$i]['start_time']=$traindata[$j]['start_time'];
           $temp1['data'][$i]['end_time']=$traindata[$j]['end_time'];
           $temp1['data'][$i]['traindata_name']=$traindata[$j]['traindata_name'];
           $temp1['data'][$i]['company']=$traindata[$j]['company'];
           $temp1['data'][$i]['traindata_address']=$traindata[$j]['traindata_address'];
           $temp1['data'][$i]['level']=$traindata[$j]['level'];

         }
      }
     }

      return $temp1;
  }
    
//----------------高校老师-----------------

    //按日期排列的所有培训信息列表与搜索
    public function ListTraindata($param)
      {
        $map=[];
        $start_time=$param['start_time'];
        $end_time=$param['end_time'];
        $issue=$param['issue'];
        $traindata_name=$param['traindata_name']; 
        $company=$param['company']; 
         if(isset($start_time)&&$start_time!='')
          {
            $map['start_time']=['between time',[$start_time,$end_time]];
          }
         if(isset($issue)&&$issue!='')
          {
            $map['issue']=$issue;
          }
         if(isset($traindata_name)&&$traindata_name!='')
          {
            $map['traindata_name']=['like', '%'.$traindata_name.'%'];
          }
         if(isset($company)&&$company!='')
          {
            $map['company']=['like', '%'.$company.'%'];
          }

        $traindata=Db::table('oa_admin_traindata')
                    ->where($map)
                    ->order('start_time')
                    ->paginate(config('pagesize'), false, ['page' => $page]);
        return $traindata;
      }
   

//----------------教育厅-----------------


  //教育厅发布培训
  public function InsertTraindata($param)
  {    
     $info['start_time']=$param['start_time'];
     $info['end_time']=$param['end_time'];
     $info['issue']=$param['issue'];
     $info['traindata_name']=$param['traindata_name'];
     $info['company']=$param['company'];
     $info['level']=$param['level'];
     $info['traindata_address']=$param['traindata_address'];

     $result=Db::table('oa_admin_traindata')
               ->where($info)
               ->select();
     if($result)
     {
      return false;
     }else{
           $info['status']=1;
        Db::table('oa_admin_traindata')
               ->insert($info);

        return true;
      }
  }
  //教育厅修改培训
  public function ReviseTraindata($param)
  {    
     $id=$param['id'];
     $info['start_time']=$param['start_time'];
     $info['end_time']=$param['end_time'];
     $info['issue']=$param['issue'];
     $info['traindata_name']=$param['traindata_name'];
     $info['company']=$param['company'];
     $info['level']=$param['level'];
     $info['traindata_address']=$param['traindata_address'];
       Db::table('oa_admin_traindata')
          ->where('id',$id)
          ->update([
            'start_time'=> $info['start_time'],
            'end_time'=> $info['end_time'],
            'issue'=>$info['issue'],
            'traindata_name'=>$info['traindata_name'],
            'company'=>$info['company'],
            'level'=>$info['level'],
            'traindata_address'=>$info['traindata_address'],
          ]);
        return true;
  }
  //删除培训信息
  public function DeleteTraindata($param)
  {    
     $result=Db::table('oa_admin_traindata')
              ->where('id',$param['id'])
              ->delete();
     if($result)
     {
         return true;
     }else{
      return false;
     }

  }
   //教育厅学校搜索与列表
    public function SchoolTraindata($param)
    {
      $sch_id=$param['sch_id'];
      $train2=Db::table('oa_admin_checkno')
             ->where('traindata_id',$param['traindata_id'])
             ->field('train_id,status,status1')
             ->select();
      for($i=0;$i<count($train2);$i++){
        $train1[$i]=$train2[$i]['train_id'];
      }
      $school=Db::table('oa_admin_train')
               ->where('id',"in",$train1)
               ->field('sch')
               ->distinct(true)
               ->select();
      $train=Db::table('oa_admin_train')
            ->where('id',"in",$train1)
            ->where('sch',$sch_id)
            ->field('id,name,preoccupation')
            ->distinct(true)
            ->select();
      for($i=0;$i<count($train);$i++){
        for($j=0;$j<count($train2);$j++){
          if($train[$i]['id']==$train2[$j]['train_id'])
          {
            $train[$i]['status']=$train2[$j]['status'];
            $train[$i]['status1']=$train2[$j]['status1'];
          }
      }
    }
      if(isset($sch_id)&&$sch_id!=''){
         return $train; 
      }else{
        return $school;
      }


       
    } 
  //教育厅和高校老师审核培训信息
  public function CheckeduTraindata($param)
  {
    $data = Db::table('oa_admin_checkno')
            ->where('train_id', $param['train_id'])
            ->where('traindata_id', $param['traindata_id'])
            ->update([
              'status' => $param['status'],
              'status1' => $param['status1'] 
            ]);
    return true;
  }

  //教育厅结束培训
  public function EduendTraindata($param)
  {
    $res=Db::table('oa_admin_traindata')
         ->where('id',$param['id'])
             ->update([
            'status'=> 2,
          ]);
    if($res)
    {
      return true;
    }else{
      return false;
    }
  }
  
}
